package com.zhike.b2b.search.index;

import java.io.File;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.FSDirectory;
import org.springframework.jdbc.core.RowMapper;
import org.wltea.analyzer.lucene.IKAnalyzer;

import com.saas.biz.paramMgr.ParamInfo;
import com.zhike.b2b.base.BaseService;
import com.zhike.b2b.search.FindAllFile;
import com.zhike.b2b.search.ReadFile;

public class FileIndexer extends BaseService
{
  public String updateFileInder(String paramString)
    throws Exception
  {
    ParamInfo localParamInfo = new ParamInfo();
    String str1 = localParamInfo.getParameter("mysqlbase.rootpath");
    File inxDir = new File(str1 + "/upload/departmentindex/" + paramString + "/index");
    FSDirectory fsdir = FSDirectory.open(inxDir);
    Analyzer analyzer = new IKAnalyzer();
    IndexWriter localIndexWriter = new IndexWriter(fsdir, analyzer, true, IndexWriter.MaxFieldLength.LIMITED);
    long l1 = new Date().getTime();
    FindAllFile findAllFile = new FindAllFile(str1 + "/upload/department/" + paramString, str1);
    ArrayList localArrayList = new ArrayList();
    localArrayList = (ArrayList)FindAllFile.pathmarry;
    for (int i = 0; i < localArrayList.size(); i++)
    {
      String str2 = "";
      String str3 = "";
      String str4 = "";
      HashMap localHashMap = (HashMap)localArrayList.get(i);
      str3 = localHashMap.get("path").toString();
      String str5 = str1 + str3;
      str4 = str5.substring(str5.lastIndexOf(".") + 1, str5.length());
      str4 = str4.toLowerCase();
      if (str4.equals("doc"))
        str2 = ReadFile.readWord(str5);
      else if (str4.equals("txt"))
        str2 = ReadFile.readTxt(str5);
      else if (str4.equals("xls"))
        str2 = ReadFile.readExcel(str5);
      else if (str4.equals("ppt"))
        str2 = ReadFile.readPpt(str5);
      File localFile2 = new File(str5);
      String str6 = localFile2.getName();
      String str7 = getfileName(str6);
      if ((str7.equals("")) || (str7 == null))
        continue;
      String str8 = str6.substring(str6.lastIndexOf(".") + 1, str6.length());
      Document localDocument = new Document();
      Field localField1 = new Field("path", str3, Field.Store.YES, Field.Index.NO);
      Field localField2 = new Field("content", str7 + str2, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS);
      Field localField3 = new Field("type", str8, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS);
      Field localField4 = new Field("title", str7, Field.Store.YES, Field.Index.NO);
      localDocument.add(localField1);
      localDocument.add(localField4);
      localDocument.add(localField2);
      localDocument.add(localField3);
      localIndexWriter.addDocument(localDocument);
    }
    localIndexWriter.optimize();
    localIndexWriter.close();
    long l2 = new Date().getTime();
    return "这花费了" + (l2 - l1) + " 毫秒来把文档增加到索引里面去!";
  }

  public String getfileName(String paramString)
  {
    String sql = "select attach_name from attach where file_path like '%" + paramString + "%'";
     List<String> ls = jt.query(sql, new RowMapper<String>(){
        public String mapRow(ResultSet rs, int arg1) throws SQLException {
			 return rs.getString(1);
		}
      });
     if(ls != null && ls.size()>0)
    	 return ls.get(0);
     else
    	 return "";
  }
}

